package com.huatai.paas.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.huatai.common.api.R;
import com.huatai.paas.dto.BackChunkDto;
import com.huatai.paas.dto.UploadFileMessage;
import com.huatai.paas.dto.info.QueryFileListInfo;
import com.huatai.paas.entity.FileListEntity;
import com.huatai.paas.service.FileChunkService;
import com.huatai.paas.vo.CheckChunkVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @BelongsProject: nanming-saas
 * @BelongsPackage: com.huatai.saas.controller
 * @Author: hecaiy
 * @CreateTime: 2024-04-11  19:05
 * @Description: TODO
 * @Version: 1.0
 */
@Api(description = "后台文件管理", tags = "后台文件管理")
@RestController
@RequestMapping("/file")
public class BackFileController {

	@Autowired
	private FileChunkService fileChunkService;

	@PostMapping("/upload/common")
	@ApiOperation("通用上传文件")
	public R upload(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "md5") String md5) throws Exception {
		return fileChunkService.upload(file,md5);
	}

	/**
	 * 文件分片上传
	 * @param chunk
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "文件分片上传", notes = "文件分片上传")
	@PostMapping("/upload")
	public R<Integer> postFileUploadMinio(BackChunkDto chunk, HttpServletResponse response) {
		if (chunk.getFile().isEmpty()) {
			throw new RuntimeException("文件不能为空");
		}
		return R.data(fileChunkService.postFileUploadMinio(chunk, response));
	}

	@ApiOperation(value = "检查文件上传状态", notes = "检查文件上传状态")
	@PostMapping("/upload/check")
	public R<CheckChunkVO> getFileUpload(@RequestBody BackChunkDto chunk, HttpServletResponse response) {
		return R.data(fileChunkService.getFileUpload(chunk, response));
	}

	@ApiOperation(value = "合并文件", notes = "合并文件")
	@PostMapping("/upload/merge")
	public R<FileListEntity> merge(@RequestBody BackChunkDto chunk) throws Exception {
		return fileChunkService.merge(chunk);
	}

	@ApiOperation("根据类型查询上传文件列表")
	@GetMapping("/query/fileList")
	public R queryFileList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
						   @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
						   @RequestParam(required = false) String fileType){
		IPage<QueryFileListInfo> pageInfo = fileChunkService.queryFileList(pageNum,pageSize,fileType);
		return R.data(pageInfo);
	}

	@ApiOperation("根据文件上传文件ID删除上传文件")
	@GetMapping("/delete/fileById")
	public R deleteFileById(@RequestParam Long id){
		boolean status = fileChunkService.deleteFileById(id);
		return R.data(status);
	}
}
